//
//  ByBallFooterView.m
//  caipiao_ios
//
//  Created by mc on 2018/8/18.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBallFooterView.h"

@implementation ByBallFooterView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = BY_BACKGROUND_COLOR;
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(18, 0, SCREEN_WIDTH - 36, 40)];
        [self addSubview:backgroundView];
        backgroundView.backgroundColor = [UIColor whiteColor];
        backgroundView.layer.cornerRadius = 3;
        
        UIView *deleteView = [[UIView alloc] init];
        [backgroundView addSubview:deleteView];
        [deleteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(backgroundView).offset(0);
            make.centerY.equalTo(backgroundView).offset(0);
            make.size.mas_equalTo(CGSizeMake(80, 20));
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [deleteView addGestureRecognizer:tap];
        
        
        UIImageView *deleteBtn = [[UIImageView alloc] init];
        [deleteView addSubview:deleteBtn];
        [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(deleteView).offset(0);
            make.centerY.equalTo(deleteView).offset(0);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        deleteBtn.image = [UIImage imageNamed:@"qingchu"];
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [deleteView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(deleteBtn.mas_right).offset(2);
            make.centerY.equalTo(deleteView).offset(0);
            make.height.mas_equalTo(@15);
            make.right.equalTo(deleteView).offset(0);
        }];
        titleLabel.text = @"清除所有";
        titleLabel.font = FONT(12);
        titleLabel.textColor = [UIColor lightGrayColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        
        
        UIImageView *bottomView = [[UIImageView alloc] init];
        [backgroundView addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(backgroundView).offset(0);
            make.left.equalTo(backgroundView).offset(0);
            make.right.equalTo(backgroundView).offset(0);
            make.height.mas_equalTo(@((SCREEN_WIDTH - 36) / 684.0 * 15));
        }];
        bottomView.image = [UIImage imageNamed:@"touzhudanbeijing下面"];
        
        
        
        
        UITextView *noteLabel = [[UITextView alloc] init];
        [self addSubview:noteLabel];
        noteLabel.text = @"我已阅读并同意《服务协议》";
        CGFloat width = [noteLabel.text getRectOfStringWithSize:CGSizeMake(1000, 20) font:12].size.width + 10;
        [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).offset(10);
            make.bottom.equalTo(self).offset(-10);
            make.height.mas_equalTo(@25);
            make.width.mas_equalTo(@(width));
        }];
        noteLabel.font = FONT(12);
        noteLabel.textColor = BY_TEXT_COLOR;
        noteLabel.backgroundColor = [UIColor clearColor];
        noteLabel.delegate = self;
        noteLabel.editable = NO;
        noteLabel.scrollEnabled = NO;
        noteLabel.textAlignment = NSTextAlignmentCenter;
        
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:noteLabel.text];
        [attributeString addAttribute:NSLinkAttributeName value:@"xieyi://" range:[noteLabel.text rangeOfString:@"《服务协议》"]];
        [attributeString addAttribute:NSForegroundColorAttributeName value:BY_Blue_COLOR range:[noteLabel.text rangeOfString:@"《服务协议》"]];
        noteLabel.attributedText = attributeString;
        
        
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:selectBtn];
        [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.right.equalTo(noteLabel.mas_left).offset(-5);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        [selectBtn setImage:[UIImage imageNamed:@"hongqiuweixuanzhong"] forState:UIControlStateNormal];
        [selectBtn setImage:[UIImage imageNamed:@"tongyi"] forState:UIControlStateSelected];
        selectBtn.selected = YES;
        [selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        self.selectBtn = selectBtn;
    }
    return self;
}


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    
    if ([[URL scheme] isEqualToString:@"xieyi"]) {
        
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/service?type=0",BOYE_WEB_BASE_URL ]];
        vc.titleString = @"服务协议";
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
        return NO;
    }
    return YES;
}


- (void)selectAction:(UIButton *)btn{
    btn.selected = !btn.selected;
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (self.deleteBlock) {
        self.deleteBlock();
    }
    
}

@end
